package com.sky.myshop.services;

import com.sky.myshop.dao.GoodsDAO;
import com.sky.myshop.dao.OrderDAO;
import com.sky.myshop.vo.Goods;
import com.sky.myshop.vo.GraphicData;
import com.sky.myshop.vo.Orders;
import org.omg.PortableServer.SERVANT_RETENTION_POLICY_ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class OrderService {
    @Autowired
    private OrderDAO odao;
    @Autowired
    private GoodsDAO gdao;

    //插入订单以及批量插入订单商品
    public void batchInsert(List<Orders> orders){
        Orders ord = new Orders();
        //获取一个oid
        odao.saveOrders(ord);
        //将oid填充到所有购买商品的id中
        for (Orders od:orders){
            od.setOid(ord.getOid());
        }
        odao.saveOrderItems(orders);
    }

    //获取图形数据源
    public GraphicData getOrderAnalyse(ShapeEnum se){
        GraphicData gd = new GraphicData();
        //将所有商品的信息存放到数组中，再将数组存放到GraphicData的XAxies属性中
        List<Goods> all = gdao.findAllGoods();
        for (Goods gs:all){
            gd.getxAxis().add(gs.getName());
        }
        //根据用户传入的饼图或柱状图利用工厂获得对应对象后再获得series数据
        ShapeSeries series = ShapeSeriesFactory.getShapeDataMode(se);
        gd.setSeries(series.getShapeData(odao.countOrderByGoodsGroup()));
        return gd;
    }
}
